package com.ymatou.iapi.godmch.testcase;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.alibaba.druid.sql.ast.statement.SQLIfStatement.Else;
import com.alibaba.druid.sql.visitor.functions.If;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymatou.iapi.godmch.parameter.GetRoleListByMchBean;
import com.ymatou.iapi.godmch.parameter.RoleDto;
import com.ymatou.iapi.godmch.parameter.createmchBean;
import com.ymatou.iapi.godmch.service.GetRoleListByMchCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.csrole;
import com.ymttest.database.sqlwapper.godmch.DefGodMchWapper;
/**
*获取商户客服角色列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetRoleListByMch{
	private static GetRoleListByMchBean getrolelistbymchBean;
	private static GetRoleListByMchCall getrolelistbymchCall;
	@BeforeClass
	public static void setUp() {
		//所有能用的角色列表现在都是默认的
		//20200826  现在所有的平台客服mchType=300
		Logger.createResultFile("获取商户客服角色列表");
	}

	@Before
	public void caseUp() {
		getrolelistbymchBean=new GetRoleListByMchBean();
		getrolelistbymchCall=new GetRoleListByMchCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getrolelistbymchCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_001() {
		Logger.start(true,"获取商户客服角色列表,成功");
		try {
			getrolelistbymchBean.setMchId(0);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetRoleListByMch_002() {
		Logger.start(true,"获取商户客服角色列表,不输入mchId失败");
		try {
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("4001", getrolelistbymchCall.getString("status"), "验证status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	//目前全局固定角色 没有定制化 后续可能会有定制化需求
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_003() {
		//csrole表  mchType=300 表示是客服权限  
		Logger.start(true,"获取商户客服角色列表,纯代理,返回纯代理权限(纯代理 查询 csrole表中mchType=2的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");//国内-纯代理
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getrolelistbymchBean.setMchId(userId_agent);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			returnVerify(300);
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_005() {
		Logger.start(true,"获取商户客服角色列表,渠道,返回渠道权限(渠道 查询 csrole表中mchType=1的角色 新增 20200610)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			getrolelistbymchBean.setMchId(userId_channl);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			returnVerify(300,false);
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_006() {
		Logger.start(true,"获取商户客服角色列表,渠道+代理,返回渠道权限(渠道 查询 csrole表中mchType=1的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("USA");
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getrolelistbymchBean.setMchId(userId_agent);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			returnVerify(300,false);
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_007() {
		Logger.start(true,"获取商户客服角色列表,Vip渠道,返回渠道权限(渠道 查询 csrole表中mchType=1的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			GodmchManageCallService.SetVip(userId_channl);
			
			getrolelistbymchBean.setMchId(userId_channl);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			returnVerify(300,false);
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_005_1() {
		Logger.start(true,"获取商户客服角色列表,渠道,返回会员制客服(渠道 查询 csrole表中mchType=1 && role=11 的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			
			//设置商户是邀请制
		    GodMemberService.setCollectionMobile(userId_channl, true);
            GodMemberService.setInvitationCodeCall(userId_channl, true, 0);
            
			getrolelistbymchBean.setMchId(userId_channl);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			
			returnVerify(300,true);
			
//			ArrayList<RoleDto> rdtos = getrolelistbymchCall.getRoleDtos();
//			//数据库的mch和对应上roleid的returndto 对比
//			 new DefGodMchWapper().selectCsroleByMchId(0).stream().filter(c->c.getMchType().equals(1) && c.getRoleId()==11).forEach(mch->{
//				 int roleId = mch.getRoleId();
//				 Logger.comment("#####   roleId   ##### "+ roleId);
//				 RoleDto returndto = rdtos.stream().filter(dto-> dto.getRoleId().equals(roleId)).findFirst().get();
//				 Logger.verifyEquals(mch.getMchId(), returndto.getMchId(), "验证mch");
//				
//				 Logger.verifyEquals(mch.getRoleId(), returndto.getRoleId(), "验证RoleId");
//				 Logger.verifyEquals(mch.getRemark(), returndto.getRemark(), "验证Remark");
//				 Logger.verifyEquals(mch.getTitle(), returndto.getTitle(), "验证Title");
//			 });
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_007_1() {
		Logger.start(true,"获取商户客服角色列表,Vip代理,返回渠道权限(Vip代理实际数据库是 代理 ,渠道 查询 csrole表中mchType=2的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			GodmchManageCallService.SetVip(userId_channl);
			
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
	//	createmchList.get(0).setCountry("CHN");//国内-纯代理
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getrolelistbymchBean.setMchId(userId_agent);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			returnVerify(300,false);
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetRoleListByMch_008() {
		Logger.start(true,"获取商户客服角色列表,国内Vip纯代理,返回代理权限(渠道 查询 csrole表中mchType=2的角色)");
		try {
			//渠道
			UserCreateBean usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_channl=DGUserCallService.UserCreateCall(usercreateBean1);
			//代理
			usercreateBean1=DGUserCallService.initUsercreateBean();
			int userId_agent=DGUserCallService.UserCreateCall(usercreateBean1);
			
			createmchBean createmchBean = GodmchManageCallService.InitcreatemchBeanForChannel(userId_channl);
			GodmchManageCallService.createmchCallForChannel(createmchBean);
			GodmchManageCallService.SetVip(userId_channl);
			
			List<createmchBean>  createmchList=GodmchManageCallService.InitcreatemchBeanForAgent(userId_channl, userId_agent);
			createmchList.get(0).setCountry("CHN");//国内-纯代理
			GodmchManageCallService.createmchCallForAgent(createmchList.toArray(new createmchBean[createmchList.size()]));
			
			getrolelistbymchBean.setMchId(userId_agent);
			getrolelistbymchCall.setData(getrolelistbymchBean);
			getrolelistbymchCall.callService();			
			Logger.verifyEquals("200", getrolelistbymchCall.getString("status"), "验证status");
			returnVerify(300);
			  
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void returnVerify(int mchType,boolean... isCode ) throws InterruptedException
	{
		ArrayList<RoleDto> rdtos = getrolelistbymchCall.getRoleDtos();
		//数据库的mch和对应上roleid的returndto 对比
		 new DefGodMchWapper().selectCsroleByMchId(0).stream().filter(c->c.getMchType().equals(mchType)).forEach(mch->{
			 int roleId = mch.getRoleId();
			 if(isCode.length>0 && isCode[0]==false && roleId==11)
			 {

			 }
			 else{
				 
				 Logger.comment("#####   roleId   ##### "+ roleId);
				
				 RoleDto returndto = rdtos.stream().filter(dto-> dto.getRoleId().equals(roleId)).findFirst().get();
				 Logger.verifyEquals(mch.getMchId(), returndto.getMchId(), "验证mchId");
				
				 Logger.verifyEquals(mch.getRoleId(), returndto.getRoleId(), "验证RoleId");
				 Logger.verifyEquals(mch.getRemark(), returndto.getRemark(), "验证Remark");
				 Logger.verifyEquals(mch.getTitle(), returndto.getTitle(), "验证Title");
			 }
		 });
	}
}